package com.hundredapp.oa.web.service;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.dao.GenericDao;
import com.hundredapp.oa.dao.SelectType;
import com.hundredapp.oa.domain.OaRole;
import com.hundredapp.oa.domain.OaUser;
import com.hundredapp.oa.repository.RoleManager;
import com.hundredapp.oa.repository.UserManager;
import com.hundredapp.oa.util.JSEscape;
import com.hundredapp.oa.util.WebUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Repository
@Scope("prototype")
public class User extends ActionSupport {

	private List<OaUser> root;

	private Object success;
	private String msg;
	private int id;
	private int limit;
	private int start;
	private int total;
	private OaUser user;
	private String roles;
	private String newPassword;
	private List<OaRole> oaroles;
	private String username;
	private String displayname;

	public OaUser getUser() {
		return user;
	}

	public void setUser(OaUser user) {
		this.user = user;
	}

	@Autowired
	private UserManager userManager;

	@Autowired
	private RoleManager roleManager;

	public String alluser() {
		String condition = SelectType.SELECT_ALL;
		if(username!=null){
			username=JSEscape.unescape(username);
			condition+=" and username like '%"+username+"%'";
		}
		if(displayname!=null){
			displayname=JSEscape.unescape(displayname);
			condition+=" and displayName like '%"+displayname+"%'";
		}
		root = userManager.findAllUser(condition, start, limit);
		total = userManager.findAllCount(condition);
		return SUCCESS;
	}

	public String add() {
		if (!roles.trim().isEmpty()) {
			user.setOaRoles(roleManager.findRolesByString(roles));
		}
		user.setCreateUserId(WebUtil.GetLoginUserId());
		user.setPassword("123456");
		user.setStatus(0);
		userManager.add(user);
		success = true;
		msg = "添加成功";
		return SUCCESS;
	}

	public String update() {
		try {
			OaUser updateuser = userManager.findUserById(id);
			if (!roles.trim().isEmpty()) {
				updateuser.setOaRoles(roleManager.findRolesByString(roles));
			}
			updateuser.setUsername(user.getUsername());
			updateuser.setSex(user.getSex());
			updateuser.setDisplayName(user.getDisplayName());
			updateuser.setPhone(user.getPhone());
			updateuser.setTelephone(user.getTelephone());
			userManager.update(updateuser);
			success = true;
			msg = "修改成功";
		} catch (Exception e) {
			success = false;
			msg = "修改失败";
		}
		return SUCCESS;
	}

	public String delete() {
		userManager.delete(id);
		success = true;
		msg = "添加成功";
		return SUCCESS;
	}

	public String find() {
		user = userManager.findUserById(id);
		oaroles = user.getOaRoles();
		return SUCCESS;
	}
	
	public String enable() {
		OaUser usr = userManager.findUserById(id);
		usr.setStatus(1);
		userManager.update(usr);
		success = true;
		msg = "启用成功";
		return SUCCESS;
	}
	
	public String disable() {
		OaUser usr = userManager.findUserById(id);
		usr.setStatus(0);
		userManager.update(usr);
		success = true;
		msg = "禁用成功";
		return SUCCESS;
	}
	
	public String reset() {
		OaUser usr = userManager.findUserById(id);
		usr.setPassword("123456");
		userManager.update(usr);
		success = true;
		msg = "重置成功";
		return SUCCESS;
	}

	public String changepassword() {
		success = false;
		Map<String, Object> session_map = ActionContext.getContext()
				.getSession();
		int id = Integer.parseInt(session_map.get("lgoin_user_id").toString());
		OaUser tempUser = userManager.findUserById(id);
		if (!tempUser.getPassword().equals(user.getPassword())) {
			msg = "旧密码输入错误，请重试。";
			return SUCCESS;
		}

		tempUser.setPassword(newPassword);
		userManager.update(tempUser);

		success = true;
		msg = "修改成功";

		return SUCCESS;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public List<OaUser> getRoot() {
		return root;
	}

	public Object getSuccess() {
		return success;
	}

	public String getMsg() {
		return msg;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public void setRoles(String roles) {
		this.roles = roles;
	}

	public void setRoleManager(RoleManager roleManager) {
		this.roleManager = roleManager;
	}

	public List<OaRole> getOaroles() {
		return oaroles;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getTotal() {
		return total;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setDisplayname(String displayname) {
		this.displayname = displayname;
	}
}
